package com.tencent.wxcloudrun.vpnmanagement.login;

import com.alibaba.fastjson.JSONObject;

import com.tencent.wxcloudrun.vpnmanagement.base.Result;
import com.tencent.wxcloudrun.vpnmanagement.login.dto.AuthDto;
import com.tencent.wxcloudrun.vpnmanagement.login.dto.LoginDto;
import com.tencent.wxcloudrun.vpnmanagement.tools.JwtUtil;
import com.tencent.wxcloudrun.vpnmanagement.tools.decryptphone.AESForWeixinGetPhoneNumber;
import com.tencent.wxcloudrun.vpnmanagement.tools.decryptphone.WeixinPhoneDecryptInfo;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import static org.omg.CORBA.ORB.init;
import static sun.security.krb5.internal.crypto.Aes256.decrypt;

@RestController
@ResponseBody
@RequestMapping("/api")
public class LoginController {
    @Value("${wxxcx.appID}")
    private String appID;
    @Value("${wxxcx.secret}")
    private String secret;

    @Autowired
    private JwtUtil jwtUtil;


    @RequestMapping("/getopenid")
    public Result getOpenId(@RequestParam("code") String code) throws IOException {
        LoginDto loginDto = new  LoginDto();
//        System.out.println(code);
//        String appID="wx77c0e20d7aa4b8ae";
//        String secret="5135f210c81dfcd1feb08661c195a0dc";
        String url= "https://api.weixin.qq.com/sns/jscode2session?appid="+appID+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        OkHttpClient client =  new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        if(response.isSuccessful()){
            String body =response.body().string();
            JSONObject jsonObjectBody = JSONObject.parseObject(body);
            loginDto.setOpenId(jsonObjectBody.getString("openid"));
            loginDto.setSessionKey(jsonObjectBody.getString("session_key"));
        }
        return Result.ok(loginDto);
    }

    @RequestMapping("/getauth")
    public Result getAuthInfo(@RequestParam("encryptedData") String encryptedData,@RequestParam("iv") String iv,@RequestParam("code") String code) throws IOException {

        String phoneNum="15022993556";

        /*   如果是企业账号，则可以获得手机号，取消注释即可，并删除59行的 String phoneNum="15022993556";*/
/*
        Result resultOpenId = this.getOpenId(code);
        System.out.println(resultOpenId);
        LoginDto loginDto= (LoginDto) resultOpenId.getData();
        String session_key = loginDto.getSessionKey();
        AESForWeixinGetPhoneNumber aes = new AESForWeixinGetPhoneNumber(encryptedData,session_key,iv);
        WeixinPhoneDecryptInfo info = aes.decrypt();
        phoneNum = info.getPhoneNumber();
*/

        String token=jwtUtil.createJwt(phoneNum,phoneNum,phoneNum,true);
        AuthDto authDto = new AuthDto(phoneNum, token);
        return Result.ok(authDto);
    }



}
